﻿using AILabelTool.Adorners;
using AILabelTool.CustomDialogs;
using AILabelTool.Models;
using AILabelTool.Models.DrawShapes;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls.Dialogs;
using AILabelTool.Expends;

namespace AILabelTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        /// <summary>
        /// 主模型
        /// </summary>
        public MainModel MainModel { get; set; }
        /// <summary>
        /// 单例
        /// </summary>
        public static MainWindow Instance { get; set; } = null;

        public MainWindow()
        {
            InitializeComponent();
            this.Closing += MainWindow_Closing;
            ProjectOptionWindow projectOptionWindow = new ProjectOptionWindow();
            projectOptionWindow.ShowDialog();
            // 保存一次
            ProjectManager.SaveProjectManager();
            Instance = this;
            MainModel = ProjectManager.Instance.MainModel;
            if (MainModel == null) this.Close();
            this.Loaded += MainWindow_Loaded;
        }
        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_Closing(object? sender, CancelEventArgs e)
        {
            if(MainModel != null)
            {
                MainModel.SaveConfig();
            }
            ProjectManager.SaveProjectManager();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = MainModel;
        }

        /// <summary>
        /// 显示等待窗口
        /// </summary>
        public void ShowWait()
        {
            BkMask.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 关闭等待窗口
        /// </summary>
        public void CloseWait()
        {
            BkMask.Visibility = Visibility.Collapsed;
        }
        /// <summary>
        /// 图片树结构的选项改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeImageListSelectChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var imageInfo = e.NewValue as ImageContentInfoModel;
            if (imageInfo is null) return;

            MainModel.CurrentImageModel = imageInfo;

        }

         private DateTime ForntDate = DateTime.Now;
        private void ClickVisiableEventMouseUp(object sender, MouseButtonEventArgs e)
        {
            DateTime now = DateTime.Now;
            if((now - ForntDate).TotalMilliseconds < 200)
            {
                (sender as UIElement).Visibility = Visibility.Visible;
            }
            ForntDate = now;
        }

        private void LoseFocusToHiddenEvent(object sender, RoutedEventArgs e)
        {
            var ui = sender as UIElement;
            ui.Visibility = Visibility.Hidden;
        }
    }
}
